﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Mvc3ToolsUpdateWeb_Default.Controllers;
using Mvc3ToolsUpdateWeb_Default.Models;
using MvcMusicStore.Services;

namespace MvcMusicStore.Tests.Unit
{
    [TestClass]
    public class AccountControllerTest
    {
        [TestMethod]
        public void LogOnWithAnInvalidModelStateReturnsTheLogOnView()
        {
            //Arrange
            AccountController controller = new AccountController();
            string returnUrl = string.Empty;
            
            //Act
            controller.ModelState.AddModelError("InvalidRequest","error");
            var viewResult = controller.LogOn(null,null) as ViewResult;
            
            //Assert
            Assert.AreEqual("LogOn", viewResult.ViewName);
        }

        [TestMethod]
        public void LogOnWithAValidModelStateCallsMembershipValidation()
        {
            //Arrange
            var model = new LogOnModel() {UserName = "Mathieu", Password = "Mon_Password@123"};
            var mockMembership = new Mock<IMembershipService>();

            mockMembership.Setup(x => x.ValidateUser(model)).Verifiable();
            AccountController controller = new AccountController() { MembershipService = mockMembership.Object };

            //Act
            controller.LogOn(model,returnUrl: String.Empty);

            //Assert
            mockMembership.VerifyAll();
        }

        [TestMethod]
        public void LogOnWithInvalidCredentialsReturnsError()
        {
            //Arrange
            var model = new LogOnModel() {UserName = "Mathieu", Password = "Mon_Password@123"};
            var mockMembership = new Mock<IMembershipService>();

            mockMembership.Setup(x => x.ValidateUser(model)).Returns(false);
            AccountController controller = new AccountController() { MembershipService = mockMembership.Object };

            //Act
            controller.LogOn(model,returnUrl:string.Empty);
           
            //Assert
            Assert.IsTrue(controller.ModelState.Select(x => x.Key == "Errors").Count() == 1);
        }
    }
}
